package cn.android.support.v7.lib.sin.crown.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseView;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * 小圆点。
 * Created by 彭治铭 on 2018/1/4.
 */

public class Dots extends BaseView {
    public Dots(Context context) {
        super(context);
        init();
    }

    public Dots(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    int defalutColor = Color.parseColor("#9FA1A0");
    int selectColor = Color.parseColor("#7160C6");
    int radius = ProportionUtils.getInstance().adapterInt(15 / 2);//园点半径
    int count = 2;//圆点个数
    int selectPosition = 0;//选中下标
    List<Integer> ArrayX = new ArrayList<>();//x数组。
    int y;//点的y值坐标

    /**
     * 初始化值
     *
     * @param radius       圆点半径
     * @param count        圆点个数
     * @param width        控件总宽度
     * @param height       控件总高度
     * @param defalutColor 圆点默认颜色
     * @param selectColor  圆点选中颜色
     */
    public void init(int radius, int count, int width, int height, int defalutColor, int selectColor) {
        this.radius = radius;
        this.count = count;
        this.width = width;
        this.height = height;
        this.defalutColor = defalutColor;
        this.selectPosition = selectColor;
        requestLayout();
    }

    /**
     * 选中指定下标的圆点。下标从0开始
     *
     * @param selectPosition
     */
    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
        invalidate();
    }

    int width = ProportionUtils.getInstance().adapterInt(88);
    int height = ProportionUtils.getInstance().adapterInt(20);

    private void init() {
        paint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(width, height);
        y = height / 2;
        int w = radius * 2 * count;
        int offse;//圆点与圆点之间的间隙。
        offse = (width - w) / (count - 1);
        ArrayX.clear();
        int x = radius;//第一个点的位置。
        for (int i = 0; i < count; i++) {
            if (i != 0) {
                x = x + offse + radius * 2;
            }
            ArrayX.add(i, x);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < count; i++) {
            if (selectPosition == i) {
                paint.setColor(selectColor);
            } else {
                paint.setColor(defalutColor);
            }
            canvas.drawCircle(ArrayX.get(i), y, radius, paint);
        }
    }

}
